"""代理配置定义模块 - 包含所有代理的配置参数。"""

from __future__ import annotations

from dataclasses import dataclass, field, fields
from typing import Annotated, Optional
from pydantic import Field, BaseModel


from langchain_core.runnables import RunnableConfig, ensure_config

from prompts.prompts import AGENT_SYSTEM, SQL_AGENT_SYSTEM



@dataclass
class ReactConfiguration:
    """The configuration for the React agent."""

    system_prompt: str = field(
        default=AGENT_SYSTEM,
        metadata={
            "description": "The system prompt to use for the agent's interactions. "
            "This prompt sets the context and behavior for the agent."
        },
    )

    model: Annotated[str, {"__template_metadata__": {"kind": "llm"}}] = field(
        default="deepseek-ai/DeepSeek-V3",
        metadata={
            "description": "The name of the language model to use for the agent's main interactions. "
            "Should be in the form: provider/model-name."
        },
    )
    max_search_results: int = field(
        default=10,
        metadata={
            "description": "The maximum number of search results to return for each search query."
        },
    )

    @classmethod
    def from_runnable_config(
        cls, config: Optional[RunnableConfig] = None
    ) -> ReactConfiguration:
        """Create a Configuration instance from a RunnableConfig object."""
        config = ensure_config(config)
        configurable = config.get("configurable") or {}
        _fields = {f.name for f in fields(cls) if f.init}
        return cls(**{k: v for k, v in configurable.items() if k in _fields})


@dataclass
class SqlConfiguration:
    """SQL 代理配置"""

    model_name: str = field(
        default="qwen2.5:7b", metadata={"description": "要使用的模型名称"}
    )

    temperature: float = field(default=0.0, metadata={"description": "模型温度参数"})

    system_prompt: str = field(
        default="""You are a helpful AI assistant with access to various tools.
        
        Available tools:
        1. search - For web searches and general information lookup
        2. user_profile_finder - For finding user profile information
        3. sql_query_tool - For executing SQL queries on databases
        
        When users ask questions that require database queries, use the sql_query_tool.
        When users need web searches or general information, use the search tool.
        When users ask about user profiles, use the user_profile_finder tool.
        
        Always provide helpful and accurate responses based on the tool results.""",
        metadata={"description": "系统提示词"},
    )

    @classmethod
    def from_runnable_config(
        cls, config: Optional[RunnableConfig] = None
    ) -> SqlConfiguration:
        """从可运行配置创建配置实例"""
        config = ensure_config(config)
        configurable = config.get("configurable") or {}
        _fields = {f.name for f in fields(cls) if f.init}
        return cls(**{k: v for k, v in configurable.items() if k in _fields})


class DocumentConfiguration(BaseModel):
    """文档解析代理配置。"""

    model_name: str = Field(default="qwen2.5:7b", description="用于文档分析的模型名称")
    chunk_size: int = Field(default=1000, description="文档分割的块大小")
    chunk_overlap: int = Field(default=200, description="文档块之间的重叠大小")


# 为了向后兼容，保留别名
Configuration = ReactConfiguration
